from types import SimpleNamespace
import pygame
from common import (
    white,
    black,
    pink,
    font_path,
    get_score,
    set_current_level,
)

screen_width, screen_height = 1300, 800

cur_image = pygame.image.load("./assets/game_5/target.png")
cur_image = pygame.transform.scale(cur_image, (450, 450))
font = pygame.font.Font(font_path, 40)

button_restart = SimpleNamespace(
    rect=pygame.Rect(520, 300, 250, 50),
    color=pink,
    text="点击重新开始",
    font_color=black,
    font=font,
    selected=-1,
)


def draw(screen):
    global selected, score
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if button_restart.rect.collidepoint(event.pos):
                set_current_level(0)
                from .main import reset
                reset()

    # 绘制背景
    screen.fill(white)
    # 绘制title

    text = font.render(
        f"最终总得分（{get_score()}）",
        True,
        black,
    )
    text_rect = text.get_rect(center=(screen_width // 2, 100))
    screen.blit(text, text_rect)

    from .main import cost_time

    text = font.render(
        f"最终用时（{cost_time:.2f}秒）",
        True,
        black,
    )
    text_rect = text.get_rect(center=(screen_width // 2, 200))
    screen.blit(text, text_rect)

    # 绘制按钮
    pygame.draw.rect(screen, button_restart.color, button_restart.rect)
    text = button_restart.font.render(
        button_restart.text, True, button_restart.font_color
    )
    text_rect = text.get_rect(center=button_restart.rect.center)
    screen.blit(text, text_rect)

    # 绘制图片
    screen.blit(cur_image, (400, 400))
